﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace KeanuMvc.Reference {

    public class Pagination {

        #region Fields
        private string strStartTag = string.Empty;
        private string strCloseTag = string.Empty;

        private string strFirst = string.Empty;
        private string strFirstBeginTag = string.Empty;
        private string strFirstEndTag = string.Empty;

        private string strPrev = string.Empty;
        private string strPrevBeginTag = string.Empty;
        private string strPrevEndTag = string.Empty;

        private string strLast = string.Empty;
        private string strLastBeginTag = string.Empty;
        private string strLastEndTag = string.Empty;

        private string strNext = string.Empty;
        private string strNextBeginTag = string.Empty;
        private string strNextEndTag = string.Empty;

        private string strNormalBeginTag = string.Empty;
        private string strNormalEndTag = string.Empty;

        private string strCurrentSelectedBeginTag = string.Empty;
        private string strCurrentSelectedEndTag = string.Empty;
        #endregion

        #region Properties
        public string StartTag {
            set {
                strStartTag = value;
            }
        }

        public string CloseTag {
            set {
                strCloseTag = value;
            }
        }

        public string First {
            set {
                strFirst = value;
            }
        }

        public string FirstBeginTag {
            set {
                strFirstBeginTag = value;
            }
        }

        public string FirstEndTag {
            set {
                strFirstEndTag = value;
            }
        }

        public string Prev {
            set {
                strPrev = value;
            }
        }

        public string PrevBeginTag {
            set {
                strPrevBeginTag = value;
            }
        }

        public string PrevEndTag {
            set {
                strPrevEndTag = value;
            }
        }

        public string Last {
            set {
                strLast = value;
            }
        }

        public string LastBeginTag {
            set {
                strLastBeginTag = value;
            }
        }

        public string LastEndTag {
            set {
                strLastEndTag = value;
            }
        }

        public string Next {
            set {
                strNext = value;
            }
        }

        public string NextBeginTag {
            set {
                strNextBeginTag = value;
            }
        }

        public string NextEndTag {
            set {
                strNextEndTag = value;
            }
        }
 
        public string NormalBeginTag {
            get {
                return strNormalBeginTag;
            }
            set {
                strNormalBeginTag = value;
            }
        }

        public string NormalEndTag {
            get {
                return strNormalEndTag;
            }
            set {
                strNormalEndTag = value;
            }
        }

        public string CurrentSelectedBeginTag {
            get {
                return strCurrentSelectedBeginTag;
            }
            set {
                strCurrentSelectedBeginTag = value;
            }
        }

        public string CurrentSelectedEndTag {
            get {
                return strCurrentSelectedEndTag;
            }
            set {
                strCurrentSelectedEndTag = value;
            }
        }
        #endregion

        public string CreatePagination(string ParramsUrl, int Start, int TotalRecord, int PerPage) {
            string PageURL = System.Web.HttpContext.Current.Request.RawUrl.ToLower();
            if (PageURL.Contains("?")) {
                PageURL = PageURL.Split('?')[0].ToString();
            }
            int showEachSide = 5;
            double doubTemp = (double)TotalRecord / PerPage;
            int maxPages = Convert.ToInt32(doubTemp.RoundUp());
            int xPages = 0;

            if ((maxPages * PerPage) > TotalRecord) {
                xPages = (maxPages * PerPage) - PerPage;
            }
            int iCur = Convert.ToInt32(Math.Round((double)(Start / PerPage) + 1)); // Current page number


            string strPaging = strStartTag;
            if (Start == 0) {
                strPaging += strFirstBeginTag + strFirst + strFirstEndTag;
            } else {
                strPaging += strFirstBeginTag + "<a href=\"" + PageURL + "?start=0" + ParramsUrl + "\">" + strFirst + "</a>" + strFirstEndTag;
            }

            if ((Start - PerPage) >= 0) {
                int next = Start - PerPage;
                if ((PerPage + 1) == Start)
                    next = 0;
                strPaging += strPrevBeginTag + "<a href=\"" + PageURL + "?start=" + next + ParramsUrl + "\">" + strPrev + "</a>" + strPrevEndTag;
            } else {
                strPaging += strPrevBeginTag + strPrev + strPrevEndTag;
            }

            int eitherSide = (showEachSide * PerPage);
            if (Start + 1 > eitherSide)
                strPaging += strNormalBeginTag + ".." + strNormalEndTag;

            int pg = 1;
            for (int y = 0; y < TotalRecord; y += PerPage) {
                int tempPage = y;
                if (y != 0) tempPage++;
                string strSelected = (tempPage == Start) ? CurrentSelectedBeginTag : NormalBeginTag;
                if ((y > (Start - eitherSide)) && (y < (Start + eitherSide))) {
                    strPaging += strSelected + "<a href=\"" + PageURL + "?start=" + tempPage.ToString() + ParramsUrl + "\">" + pg + "</a>" + NormalEndTag;
                }
                pg++;
            }


            if ((Start + eitherSide) < TotalRecord)
                strPaging += strNormalBeginTag + ".." + strNormalEndTag;

            if ((Start + PerPage) < TotalRecord) {
                int tempPage = Start;
                if (Start == 0)
                    tempPage++;
                strPaging += strNextBeginTag + "<a href=\"" + PageURL + "?start=" + (tempPage + PerPage).ToString() + ParramsUrl + "\">" + strNext + "</a>" + strNextEndTag;
            } else {
                strPaging += strNextBeginTag + strNext + strNextEndTag;
            }

            if (Start != xPages) {
                int tempPage = xPages;
                strPaging += strLastBeginTag + "<a href=\"" + PageURL + "?start=" + tempPage.ToString() + ParramsUrl + "\">" + strLast + "</a>" + strLastEndTag;
            } else {
                strPaging += strLastBeginTag + strLast + strLastEndTag;
            }

            return strPaging;
        }
    }
}
